package org.catdroid.bomba.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.catdroid.bomba.classes.Game;
import org.catdroid.bomba.classes.Player;
import org.catdroid.bomba.classes.Players;
import org.catdroid.bomba.classes.Response;

import android.util.Log;

import com.google.gson.Gson;

public class Connect2Server {

	public static void setClientServer(Player player) throws ClientProtocolException, IOException{
		afegirPlayer(player.getId(), player.getName());
	}
	
	public Game getGame(){
		
		
		return null;
		
	}
	public void setUser(){
		
	}

	
	//OFF-INICI
	static Players llista;
	static HttpUtil hu;
	public static void afegirPlayer(String id,String name) throws ClientProtocolException, IOException {
		hu = new HttpUtil();
		DefaultHttpClient httpclient = hu.getNewHttpClient();

		// Prepare a request object
		HttpPost post = new HttpPost( Constants.URL_PLAYER);
		Log.i("HTTP",Constants.URL_PLAYER);
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("id", id));
		pairs.add(new BasicNameValuePair("name",name));

		post.setEntity(new UrlEncodedFormEntity(pairs));

		// Execute the request
		HttpResponse response = httpclient.execute(post);
		//String responseBody = EntityUtils.toString(response.getEntity());
	}
	
	public static void activatePlayer(String id) throws ClientProtocolException, IOException 
	{
		if (hu == null)
		{
			hu = new HttpUtil();
		}
		DefaultHttpClient httpclient = hu.getNewHttpClient();

		// Prepare a request object
		HttpPost post = new HttpPost( Constants.URL_START);
		Log.i("HTTP",Constants.URL_START);
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("id", id));

		post.setEntity(new UrlEncodedFormEntity(pairs));

		// Execute the request
		HttpResponse response = httpclient.execute(post);
		//String responseBody = EntityUtils.toString(response.getEntity());
	}
	
	public static boolean goGame() throws ClientProtocolException, IOException
	{
		// Indica el esatdo de los jugadores, si todos estan Go! se puede empezar la partida
		if (hu == null)
		{
			hu = new HttpUtil();
		}
		
		DefaultHttpClient httpclient = hu.getNewHttpClient();

		// Prepare a request object
		HttpGet get = new HttpGet(Constants.URL_START);
		Log.i("HTTP",Constants.URL_START);

		// Execute the request
		HttpResponse response = httpclient.execute(get);
		String responseBody = EntityUtils.toString(response.getEntity());
		
		Gson gson = new Gson();
		
		Response resp = gson.fromJson(responseBody, Response.class);
		
		
		
		return resp.isOk();
	}
	
	public static Players getAllPlayers() {
		if (hu == null)
		{
			hu = new HttpUtil();
		}
		try {
			llista = (Players) hu.getAll(Players.class, Constants.URL_PLAYER);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		}
		return llista;
	}
	//FI-OFF

}
